﻿using Opo.Net.Mail;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Extension methods for Opo.Perspective.Core
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Converts an IMailMessage to an IPerspectiveEntity.
        /// </summary>
        /// <param name="mailMessage">The mail message.</param>
        /// <returns></returns>
        public static IPerspectiveEntity ToPerspectiveMessage(this IMailMessage mailMessage)
        {
            return (IPerspectiveEntity)mailMessage;
        }
        /// <summary>
        /// Converts an IMailMessage to an IPerspectiveEntity.
        /// </summary>
        /// <param name="mailMessage">The mail message.</param>
        /// <param name="flag">Sets a flag.</param>
        /// <param name="isRead">A value indicating whether the entity has been read.</param>
        /// <param name="tags">The tags.</param>
        /// <returns></returns>
        public static IPerspectiveEntity ToPerspectiveMessage(this IMailMessage mailMessage, Flag flag, bool isRead, TagCollection tags)
        {
            IPerspectiveEntity perspectiveEntity = mailMessage.ToPerspectiveMessage();
            perspectiveEntity.Flag = flag;
            perspectiveEntity.IsRead = isRead;
            perspectiveEntity.Tags = tags;
            return perspectiveEntity;
        }


        /// <summary>
        /// Gets the items from the specified repository filtered by the predicate.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="repository">The repository.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns></returns>
        public static List<T> Get<T>(this IRepository<T> repository, Func<T, bool> predicate)
        {
            List<T> filteredItems;
            if (predicate != null)
            {
                filteredItems = (from i in repository.GetAll()
                                 where predicate.Invoke(i)
                                 select i).ToList();
            }
            else
            {
                filteredItems = repository.GetAll().ToList();
            }
            return filteredItems;
        }
    }
}
